package org.rsbot.gui;

import java.util.Hashtable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.net.MalformedURLException;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.rsbot.client.input.Canvas;
import org.rsbot.util.GlobalConfiguration;

/**
 * A dialog to set the frame rate delay in CanvasWrapper.
 * @author Sorcermus
 */
public class ImageFrameDelay extends JInternalFrame {

    private static final long serialVersionUID = 1L;
    private final Logger log = Logger.getLogger(ImageFrameDelay.class.getName());
    private JSlider slider;
    private String filename;
    static File ImageDelayFile;
    public static int slowGraphicsTime;
    private JButton confirmationButton;
    private JLabel jLabel1 = new JLabel();
    private JLabel jLabel10 = new JLabel();
    private JLabel jLabel11 = new JLabel();
    private JLabel jLabel12 = new JLabel();
    private JLabel jLabel2 = new JLabel();
    private JLabel jLabel3 = new JLabel();
    private JLabel jLabel4 = new JLabel();
    private JLabel jLabel5 = new JLabel();
    private JLabel jLabel6 = new JLabel();
    private JLabel jLabel7 = new JLabel();
    private JLabel jLabel8 = new JLabel();
    private JLabel jLabel9 = new JLabel();
    private JPanel jPanel1 = new JPanel();
    private JPanel jPanel2 = new JPanel();

    public ImageFrameDelay() {
        
        setFrameIcon(new ImageIcon(GlobalConfiguration.getImage(GlobalConfiguration.Paths.Resources.DELAY, GlobalConfiguration.Paths.DELAY)));

        setTitle(("Image Frame Delay Rate: " + slowGraphicsTime + " ms"));

        jPanel1.setBorder(BorderFactory.createTitledBorder(" Image Delay Settings "));
        jPanel1.setRequestFocusEnabled(false);

        jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel2.setText("100");

        jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel3.setText("0");

        slider = new JSlider(0, 100, slowGraphicsTime);
        slider.setOrientation(JSlider.VERTICAL);
        slider.setPaintLabels(true);
        slider.setPaintTicks(true);

        Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>();
        table.put(0, new JLabel(" Fast"));
        table.put(100, new JLabel("Slow "));
        slider.setLabelTable(table);

        slider.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                setTitle("Image Frame Delay Rate: " + slider.getValue() + " ms");
            }
        });

        GroupLayout jPanel2Layout = new GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(jLabel2, GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE).addComponent(jLabel3, GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE).addComponent(slider, GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE));
        jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel2).addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(slider, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE).addComponent(jLabel3)));

        jLabel1.setText("Slow Graphics / Use Less CPU");

        jLabel4.setText("Use this slide bar to");

        jLabel5.setText("slow / speed up how");

        jLabel6.setText("your graphics show");

        jLabel7.setText("on the client.");

        jLabel8.setText("Using slow graphics");

        jLabel9.setText("might cause some");

        jLabel10.setText("scripts to not work");

        jLabel11.setText("properly...");

        jLabel12.setText("Fast Graphics / Use More CPU");

        confirmationButton = new JButton("Save");
        confirmationButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Canvas.SLOW_GRAPHICS_DELAY = slider.getValue();
            }
        });

        GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addComponent(jPanel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(jLabel5, GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE).addComponent(jLabel4, GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE).addComponent(jLabel6, GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE).addComponent(jLabel7, GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE).addComponent(jLabel8, GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE).addComponent(jLabel9, GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE).addComponent(jLabel10, GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE).addComponent(jLabel11, GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE))).addComponent(jLabel1, GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE).addComponent(jLabel12, GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE).addComponent(confirmationButton, GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)).addContainerGap()));
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel1).addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap(11, Short.MAX_VALUE).addComponent(jPanel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(jPanel1Layout.createSequentialGroup().addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel4).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel5).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel6).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel7).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel8).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel9).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel10).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel11))).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel12).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(confirmationButton).addContainerGap(43, Short.MAX_VALUE)));

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap(18, Short.MAX_VALUE).addComponent(jPanel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addContainerGap(10, Short.MAX_VALUE)));
        layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, 400, GroupLayout.PREFERRED_SIZE).addContainerGap(5, Short.MAX_VALUE)));

        pack();
        setVisible(true);
    }
}
